import json

rating_list = []
with open('ratings_cleaned.dat', 'r') as f:
    for line in f:
        line = line.strip('\n')
        rating_split = line.split('\t')
        user = rating_split[0]
        item = rating_split[1]
        rating = rating_split[2]
        rating_list.append(rating_split)

movie_list = []
with open('movie_data_cleaned', 'r') as w:
    for line in w:
        movie = json.loads(line)
        movie_list.append(movie)


movie_rating_list = []
for rating in rating_list:
    movie_rating = {}
    for movie in movie_list:
        if rating[1] == movie['id']:
            movie_rating['user'] = rating[0]
            movie_rating['movie'] = rating[1]
            movie_rating['rating'] = rating[2]
            movie_rating['critic_rating'] = movie['critic_rating']
            movie_rating['audience_rating'] = movie['audience_rating']
            movie_rating['director'] = movie['director']
            movie_rating['country'] = movie['country']
            movie_rating['genres'] = movie['genres']
            movie_rating_list.append(movie_rating)
            break

with open('movie_rating_entity.dat', 'w') as q:
    for line in movie_rating_list:
        print(line)
        line = json.dumps(line)
        q.write(line)
        q.write('\n')